package cn.sdhuijin.healthexam.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.bean.Item;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 导出列表
 * 
 * @author sunping
 * 
 */
public class ExportDateListActivity extends AbstractActivity implements
		OnClickListener, OnCheckedChangeListener {

	ListView lv;
	List<Item> items = new ArrayList<Item>();
	Context mContext;
	MyListAdapter adapter;
	List<Integer> listItemID = new ArrayList<Integer>();
	private DatabaseHelper dbh;
	private String exportSql;
	private CheckBox cbAll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_export_data_list);
		ExitApplication.getInstance().addActivity(this);

		dbh = new DatabaseHelper(this);

		// 获取intent实例
		Intent intent = getIntent();
		// 获取传递过来的sql语句
		exportSql = intent.getStringExtra("sql");
		mContext = getApplicationContext();
		lv = (ListView) findViewById(R.id.search_result_list);

		initItemData(exportSql);
		adapter = new MyListAdapter(items);
		lv.setAdapter(adapter);

		setButtonListener(this, R.id.btn_back, R.id.btn_export);
		cbAll = (CheckBox) findViewById(R.id.select_all);
		cbAll.setOnCheckedChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		int clickId = v.getId();
		switch (clickId) {
		case R.id.btn_export:
			exportExcel();
			break;
		case R.id.btn_back:
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton cb, boolean flag) {
		int ckId = cb.getId();
		switch (ckId) {
		case R.id.select_all:
			if (flag == true) {
				for (int i = 0; i < items.size(); i++) {
					adapter.mChecked.set(i, true);
				}
			} else {
				for (int i = 0; i < items.size(); i++) {
					adapter.mChecked.set(i, false);
				}
			}
			dataChanged();
			break;
		default:
			break;
		}

	}

	/**
	 * 填充列表数据
	 */
	private void initItemData(String sql) {
		items = dbh.searchList(sql);
	}

	// 刷新listview
	private void dataChanged() {
		// 通知listView刷新
		adapter.notifyDataSetChanged();
	};

	// 把字符串转为日期
	public static String ConverToDate(String strDate) throws Exception {
		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df1.parse(strDate);
		DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df2.format(date);
	}

	// 自定义ListView适配器
	class MyListAdapter extends BaseAdapter {
		private List<Boolean> mChecked;
		List<Item> listItem;

		private static final int NAME_TAG = 0;
		private static final int CARDID_TAG = 1;
		private static final int SEX_TAG = 2;
		private static final int BIRTHDATE_TAG = 3;
		private static final int STYLE_TAG = 4;
		private static final int DOCTOR_TAG = 5;
		private static final int INPUTDATE_TAG = 6;

		HashMap<Integer, View> map = new HashMap<Integer, View>();
		private LayoutInflater mInflater;

		public MyListAdapter(List<Item> list) {
			listItem = new ArrayList<Item>();
			listItem = list;

			mChecked = new ArrayList<Boolean>();
			for (int i = 0; i < list.size(); i++) {
				mChecked.add(false);
			}
		}

		@Override
		public int getCount() {
			return listItem.size();
		}

		@Override
		public Object getItem(int position) {
			return listItem.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				mInflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = mInflater.inflate(R.layout.activity_export_item,
						null);
				holder = new ViewHolder();
				holder.selected = (CheckBox) convertView
						.findViewById(R.id.select);
				holder.name = (TextView) convertView.findViewById(R.id.item01);
				holder.name.setTag(NAME_TAG);
				holder.cardId = (TextView) convertView
						.findViewById(R.id.item02);
				holder.name.setTag(CARDID_TAG);
				holder.sex = (TextView) convertView.findViewById(R.id.item03);
				holder.name.setTag(SEX_TAG);
				holder.birthDate = (TextView) convertView
						.findViewById(R.id.item04);
				holder.name.setTag(BIRTHDATE_TAG);
				holder.style = (TextView) convertView.findViewById(R.id.item05);
				holder.name.setTag(STYLE_TAG);
				holder.doctor = (TextView) convertView
						.findViewById(R.id.item06);
				holder.name.setTag(DOCTOR_TAG);
				holder.inputDate = (TextView) convertView
						.findViewById(R.id.item07);
				holder.name.setTag(INPUTDATE_TAG);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.selected.setChecked(mChecked.get(position));
			holder.name.setText(listItem.get(position).getName());
			holder.name.setTextColor(android.graphics.Color.RED);
			holder.cardId.setText(listItem.get(position).getCardId());
			holder.sex.setText(listItem.get(position).getSex());
			holder.birthDate.setText((CharSequence) listItem.get(position)
					.getBirthDate());
			holder.style.setText(listItem.get(position).getStyle());
			holder.inputDate.setText(Util.formatDateTime(listItem.get(position)
					.getInputDate()));
			holder.doctor.setText(listItem.get(position).getDoctor());

			holder.selected.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					CheckBox cb = (CheckBox) v;
					mChecked.set(position, cb.isChecked());
				}
			});
			holder.name.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					String fileName = listItem.get(position).getInputDate();
					Log.i("位置", position + "  " + fileName);
					boolean fileExist = checkFileExist(fileName);
					if (fileExist) {
						String dataType = listItem.get(position).getStyle();
						startActivityByDataType(dataType, fileName);
					} else {
						ExportDateListActivity.this
								.toastInformation(R.string.record_deleted);
					}
				}
			});

			return convertView;
		}
	}

	private boolean checkFileExist(String fileName) {
		File file = null;
		boolean exist = false;
		try {
			file = new File(Environment.getExternalStorageDirectory().getPath()
					+ "/health/" + fileName + ".txt");
			if (file.exists()) {
				exist = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return exist;
	}

	/**
	 * 通过不同数据类型启动不同Activity
	 * 
	 * @param dataType
	 *            数据类型
	 * @param fileName
	 *            文件名称
	 */
	private void startActivityByDataType(String dataType, String fileName) {
		Intent intent = new Intent();
		intent.putExtra("filename", fileName);
		if ("档案".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ArchivesInfoActivity.class);
		} else if ("体检".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					HealthExamInfoActivity.class);
		} else if ("精神病随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					MentalVisitInfoActivity.class);
		} else if ("高血压随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					HypertensionVisitInfoActivity.class);
		} else if ("糖尿病随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					DiabetesInfoActivity.class);
		} else if ("中医体质判定".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					BodyJudgeInfoActivity.class);
		} else if ("产前第一次随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ChanQianInfoActivity.class);
		} else if ("产前第二次随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					SecondChanQianInfoActivity.class);
		} else if ("产前第三次随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ThirdChanQianInfoActivity.class);
		} else if ("产前第四次随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ForthChanQianInfoActivity.class);
		} else if ("产前第五次随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					FifthChanQianInfoActivity.class);
		} else if ("产后随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ChanHouInfoActivity.class);
		} else if ("产后四十二天随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ChanHouSiShiErInfoActivity.class);
		} else if ("新生儿随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					NewChildrenVisitInfoActivity.class);
		} else if ("新生儿满月随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ManYueVisitInfoActivity.class);
		} else if ("新生儿三月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					SanYueVisitInfoActivity.class);
		} else if ("新生儿六月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					LiuYueVisitInfoActivity.class);
		} else if ("新生儿八月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					BaYueVisitInfoActivity.class);
		} else if ("新生儿十二月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ShiErYueVisitInfoActivity.class);
		} else if ("新生儿十八月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ShiBaYueVisitInfoActivity.class);
		} else if ("新生儿二十四月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					ErShiSiYueVisitInfoActivity.class);
		} else if ("新生儿三十月龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					SanShiYueVisitInfoActivity.class);
		} else if ("新生儿三岁龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					SanSuiVisitInfoActivity.class);
		} else if ("新生儿四岁龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					SiSuiVisitInfoActivity.class);
		} else if ("新生儿五岁龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					WuSuiVisitInfoActivity.class);
		} else if ("新生儿六岁龄随访".equals(dataType)) {
			intent.setClass(ExportDateListActivity.this,
					LiuSuiVisitInfoActivity.class);
		}
		startActivity(intent);
	}

	static class ViewHolder {
		CheckBox selected;
		TextView name;
		TextView cardId;
		TextView sex;
		TextView birthDate;
		TextView style;
		TextView inputDate;
		TextView doctor;
	}

	/**
	 * 导出列表数据到SD卡中
	 */
	public void exportExcel() {
		boolean result = false;
		try {

			Log.i(this.getClass().getSimpleName(), "导出excel到FTP---开始");
			File file = new File(Environment.getExternalStorageDirectory()
					+ "/health/out.xls");
			if (!file.exists()) {
				file.createNewFile();
			}
			String path = file.getAbsolutePath();
			FileOutputStream os = new FileOutputStream(path);
			WritableWorkbook wbook = Workbook.createWorkbook(os);
			// Cursor cur = dbh.searchExport(exportSql);
			int i = 0;
			int j = 0;
			int k = 0;
			int l = 0;
			int m = 0;
			int n = 0;
			int o = 0;
			int p = 0;
			int q = 0;
			int r = 0;
			int s = 0;
			int t = 0;
			int u = 0;
			int v = 0;
			int w = 0;
			int x = 0;
			int y = 0;
			int z = 0;
			int a = 0;
			int b = 0;
			int c = 0;
			int d = 0;
			int e = 0;
			int f = 0;
			int g = 0;
			int h = 0;

			WritableSheet sheetOfArchivesInfo = wbook.createSheet("档案信息", 0);
			WritableSheet sheetOfExamInfo = wbook.createSheet("体检信息", 1);
			WritableSheet sheetOfDiabetesInfo = wbook.createSheet("糖尿病随访信息", 2);
			WritableSheet sheetOfHypertensionInfo = wbook.createSheet(
					"高血压随访信息", 3);
			WritableSheet sheetOfMentalInfo = wbook.createSheet("精神病随访信息", 4);
			WritableSheet sheetOfZhongyiInfo = wbook.createSheet("中医体质判定信息", 5);
			WritableSheet sheetOfChanQianFirstInfo = wbook.createSheet(
					"产前第一次随访", 6);
			WritableSheet sheetOfChanQianSecondInfo = wbook.createSheet(
					"产前第二次随访", 7);
			WritableSheet sheetOfChanQianThirdInfo = wbook.createSheet(
					"产前第三次随访", 8);
			WritableSheet sheetOfChanQianForthInfo = wbook.createSheet(
					"产前第四次随访", 9);
			WritableSheet sheetOfChanQianFifthInfo = wbook.createSheet(
					"产前第五次随访", 10);
			WritableSheet sheetOfChanHouInfo = wbook.createSheet("产后随访", 11);
			WritableSheet sheetOfChanHouSiShiErInfo = wbook.createSheet(
					"产后四十二天随访", 12);
			WritableSheet sheetOfNewChildInfo = wbook.createSheet("新生儿随访", 13);
			WritableSheet sheetOfNewChildManYueInfo = wbook.createSheet(
					"新生儿满月随访", 14);
			WritableSheet sheetOfNewChildSanYueInfo = wbook.createSheet(
					"新生儿三月龄随访", 15);
			WritableSheet sheetOfNewChildLiuYueInfo = wbook.createSheet(
					"新生儿六月龄随访", 16);
			WritableSheet sheetOfNewChildBaYueInfo = wbook.createSheet(
					"新生儿八月龄随访", 17);
			WritableSheet sheetOfNewChildShiErYueInfo = wbook.createSheet(
					"新生儿十二月龄随访", 18);
			WritableSheet sheetOfNewChildShiBaYueInfo = wbook.createSheet(
					"新生儿十八月龄随访", 19);
			WritableSheet sheetOfNewChildErShiSiYueInfo = wbook.createSheet(
					"新生儿二十四月龄随访", 20);
			WritableSheet sheetOfNewChildSanShiYueInfo = wbook.createSheet(
					"新生儿三十月龄随访", 21);
			WritableSheet sheetOfNewChildSanSuiInfo = wbook.createSheet(
					"新生儿三岁龄随访", 22);
			WritableSheet sheetOfNewChildSiSuiInfo = wbook.createSheet(
					"新生儿四岁龄随访", 23);
			WritableSheet sheetOfNewChildWuSuiInfo = wbook.createSheet(
					"新生儿五岁龄随访", 24);
			WritableSheet sheetOfNewChildLiuSuiInfo = wbook.createSheet(
					"新生儿六岁龄随访", 25);

			boolean flag = false;
			// 遍历游标，取出每一行的值即文件名
			for (int index = 0; index < adapter.mChecked.size(); index++) {

				if (adapter.mChecked.get(index) == true) {
					flag = true;
					Item item = (Item) adapter.getItem(index);
					String fileName = item.getInputDate();
					String style = item.getStyle();

					// 若果记录类型为档案，导出到档案sheet
					if (style.equals("档案")) {
						i += 1;
						String archivesInfo[] = { "姓名", "编号", "性别", "出生日期",
								"身份证号", "家庭地址", "工作单位", "联系电话", "紧急联系人",
								"联系人电话", "常住类型", "民族", "血型", "RH阴性", "文化程度",
								"职业", "婚姻状况", "医药支付", "药物过敏", "其他药物过敏史", "暴露史",
								"疾病史", "其他疾病史", "高血压确诊时间", "糖尿病确诊时间",
								"冠心病确诊时间", "慢性阻塞性肺疾病确诊时间", "恶性肿瘤确诊时间",
								"脑卒中确诊时间", "重性精神疾病确诊时间", "结核病确诊时间", "肝炎确诊时间",
								"其他法定传染病确诊时间", "职业病确诊时间", "其他疾病史确诊时间", "手术名称1",
								"手术日期1", "手术名称2", "手术日期2", "外伤名称1", "外伤日期1",
								"外伤名称2", "外伤日期2", "输血原因1", "输血日期1", "输血原因2",
								"输血日期2", "家族病史_父亲", "其他家族病史_父亲", "家族病史_母亲",
								"其他家族病史_母亲", "家族病史_兄弟姐妹", "其他家族病史_兄弟姐妹",
								"家族病史_子女", "其他家族病史_子女", "遗传病史", "遗传病名称",
								"残疾情况", "其他残疾", "厨房排风设施", "燃料类型", "饮水", "厕所",
								"禽畜栏" };
						exportArchivesInfo(sheetOfArchivesInfo, fileName, i,
								archivesInfo);
					}

					if (style.equals("体检")) {
						j += 1;
						String examInfo[] = { "姓名", "编号", "身份证号", "体检日期",
								"责任医生", "症状", "症状-其他", "体温", "脉率", "呼吸频率",
								"左侧高压", "左侧低压", "右侧高压", "右侧低压", "身高", "体重",
								"腰围", "体质指数", "健康状态", "生活自理能力", "认知功能",
								"简易智力状态检查评分", "老人年抑郁检查评分", "情感状态", "锻炼频率",
								"每次锻炼时间", "坚持锻炼时间", "锻炼方式", "饮食习惯", "吸烟状况",
								"日吸烟量", "开始吸烟年龄", "戒烟年龄", "饮酒频率", "日饮酒量",
								"是否戒酒", "戒酒年龄", "开始饮酒年龄", "近一年内是否曾醉酒", "饮酒种类",
								"其他酒类", "职业病危害因素接触史", "毒物种类粉尘防护措施", "放射物质防护措施",
								"工种从业时间", "物理因素防护措施", "化学物质防护措施", "其他防护措施",
								"口唇", "齿列", "咽部", "左眼视力", "右眼视力", "左眼矫正视力",
								"右眼矫正视力", "听力", "运动功能", "眼底", "皮肤", "巩膜",
								"巩膜-其他", "淋巴结", "淋巴结-其他", "桶状胸", "罗音", "罗音-其他",
								"呼吸音", "心率", "心律", "杂音", "压痛", "包块", "肝大",
								"脾大", "移动性浊音", "下肢水肿", "足背动脉搏动", "肛门指诊",
								"肛门指诊-其他", "乳腺", "乳腺-其他", "外阴", "阴道", "宫颈",
								"宫体", "附件", "查体其他", "血红蛋白", "白细胞", "血小板",
								"血常规其他", "尿蛋白", "尿糖", "尿酮体", "尿潜血", "尿常规其他",
								"空腹血糖", "心电图", "尿微量白蛋白", "大便潜血", "糖化血红蛋白",
								"乙型肝炎表面抗原", "血清谷丙转氨酶", "血清谷草转氨酶", "白蛋白",
								"总胆红素", "结合胆红素", "血清肌酐", "血尿素氮", "血钾浓度",
								"血钠浓度", "总胆固醇", "甘油三酯", "血清低密度脂蛋白胆固醇",
								"血清高密度脂蛋白胆固醇", "辅助检查其他", "胸部X线片", "B超", "宫颈涂片",
								"平和质", "气虚质", "阳虚质", "阴虚质", "痰湿质", "湿热质",
								"血瘀质", "气郁质", "特秉质", "脑血管疾病", "脑血管疾病-其他",
								"肾脏疾病", "肾脏疾病-其他", "心脏疾病", "心脏疾病-其他", "血管疾病",
								"血管疾病-其他", "眼部疾病", "眼部疾病-其他", "神经系统疾病",
								"神经系统疾病名称", "其他系统疾病", "其他系统疾病名称", "入院日期",
								"出院日期", "住院原因", "医疗机构名称", "住院史病案号", "建床日期",
								"撤床日期", "家庭病床原因", "家庭病床医疗机构名称", "家庭病案病案号",
								"药物名称1", "用法1", "用量1", "用药时间1", "服药依从性1",
								"药物名称2", "用法2", "用量2", "用药时间2", "服药依从性2",
								"药物名称3", "用法3", "用量3", "用药时间3", "服药依从性3",
								"药物名称4", "用法4", "用量4", "用药时间4", "服药依从性4",
								"药物名称5", "用法5", "用量5", "用药时间5", "服药依从性5",
								"药物名称6", "用法6", "用量6", "用药时间6", "服药依从性6",
								"预防接种名称1", "预防接种日期1", "预防接种机构1", "预防接种名称2",
								"预防接种日期2", "预防接种机构2", "健康评价", "健康评价-异常1",
								"健康评价-异常2", "健康评价-异常3", "健康评价-异常4", "健康指导",
								"危险因素控制", "危险因素控制-其他" };
						exportArchivesInfo(sheetOfExamInfo, fileName, j,
								examInfo);
					}
					if (style.equals("糖尿病随访")) {
						k += 1;
						String diabetesInfo[] = { "姓名", "编号", "身份证号", "随访日期",
								"随访方式", "高压", "低压", "体重", "体质指数", "足背动脉搏动",
								"体征其他", "症状", "症状其他", "日吸烟量", "日饮酒量", "运动次数",
								"运动时间", "主食", "心理调整", "遵医行为", "服药依从性",
								"药物不良反应", "低血糖反应", "空腹血糖值", "此次随访分类", "糖化血红蛋白",
								"糖化血红蛋白检查日期", "药物名称一", "药物一次数", "药物一用量",
								"药物名称二", "药物二次数", "药物二用量", "药物名称三", "药物三次数",
								"药物三用量", "胰岛素种类", "胰岛素用法和用量", "转诊原因",
								"转诊机构及科别", "下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfDiabetesInfo, fileName, k,
								diabetesInfo);
					}
					if (style.equals("高血压随访")) {
						l += 1;
						String hypertensionInfo[] = { "姓名", "编号", "身份证号",
								"随访日期", "随访方式", "高压", "低压", "体重", "体质指数", "心率",
								"体征其他", "症状", "症状其他", "日吸烟量", "日饮酒量", "运动次数",
								"运动时间", "摄盐情况", "心理调整", "遵医行为", "服药依从性",
								"药物不良反应", "辅助检查", "此次随访分类", "药物名称一", "药物一次数",
								"药物一用量", "药物名称二", "药物二次数", "药物二用量", "药物名称三",
								"药物三次数", "药物三用量", "其他药物次数", "其他药物次数", "其他药物用量",
								"转诊原因", "转诊机构及科别", "下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfHypertensionInfo, fileName,
								l, hypertensionInfo);
					}
					if (style.equals("精神病随访")) {
						m += 1;
						String mentalInfo[] = { "姓名", "编号", "身份证号", "随访日期",
								"随访医生签名", "危险性", "目前症状", "其它目前症状", "自知力",
								"睡眠情况", "饮食情况", "个人生活料理", "家务劳动", "生产劳动及工作",
								"学习能力", "社会人际交往", "轻度滋事", "肇事", "肇祸", "自伤",
								"自杀未遂", "关锁情况", "住院情况", "末次住院时间", "实验室检查",
								"服药依从性", "药物不良反应", "治疗效果", "转诊原因", "机构及科别",
								"药物名称一", "药物一次数", "药物一用量", "药物名称二", "药物二次数",
								"药物二用量", "药物名称三", "药物三次数", "药物三用量", "康复措施",
								"其它康复措施", "此次随访分类", "下次随访日期", "指导意见" };
						exportArchivesInfo(sheetOfMentalInfo, fileName, m,
								mentalInfo);
					}
					if (style.equals("中医体质判定")) {
						n += 1;
						String zhongyiInfo[] = { "姓名", "编号", "身份证号", "随访日期",
								"随访医师", "体质类型", "体质特征", "调节建议" };
						exportArchivesInfo(sheetOfZhongyiInfo, fileName, n,
								zhongyiInfo);
					}
					if (style.equals("产前第一次随访")) {
						o += 1;
						String chanQianFirstInfo[] = { "姓名", "身份证号", "年龄",
								"填表孕周", "阴道分娩次", "剖宫产次", "末次月经", "预产期", "身高",
								"体重", "体质指数", "高压", "低压", "丈夫姓名", "丈夫年龄",
								"丈夫电话", "心脏", "心脏异常", "肺部", "肺部异常", "既往史",
								"其它既往史", "家族史", "其它家族史", "个人史", "其它个人史", "孕产史",
								"妇科手术史", "外阴", "外阴异常", "阴道", "阴道异常", "宫颈",
								"宫颈异常", "子宫", "子宫异常", "附件", "附件异常", "血红蛋白值",
								"白细胞计数值", "血小板计数值", "血常规其他", "尿蛋白", "尿糖",
								"尿酮体", "尿潜血", "尿常规其他", "血型", "血糖", "血清谷丙转氨酶",
								"血清谷草转氨酶", "白蛋白", "总胆红素", "结合胆红素", "血清肌酐",
								"血尿素氮", "阴道分泌物", "其他阴道分泌物", "阴道清洁度",
								"乙型肝炎表面抗原", "乙型肝炎表面抗体", "乙型肝炎e抗原", "乙型肝炎e抗体",
								"乙型肝炎核心抗体", "梅毒血清学试验", "HIV抗体检测", "保健指导",
								"其它保健指导", "总体评估", "转诊", "转诊原因", "机构及科室",
								"下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanQianFirstInfo, fileName,
								o, chanQianFirstInfo);
					}
					if (style.equals("产前第二次随访")) {
						p += 1;
						String chanQianSecondInfo[] = { "姓名", "身份证号", "孕周",
								"主诉", "体重", "宫底高度", "腹围", "胎位", "胎心率", "高压",
								"低压", "血红蛋白", "尿蛋白", "其他辅助检查", "分类", "分类异常",
								"指导", "其它指导", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanQianSecondInfo, fileName,
								p, chanQianSecondInfo);
					}
					if (style.equals("产前第三次随访")) {
						f += 1;
						String chanQianThirdInfo[] = { "姓名", "身份证号", "孕周",
								"主诉", "体重", "宫底高度", "腹围", "胎位", "胎心率", "高压",
								"低压", "血红蛋白", "尿蛋白", "其他辅助检查", "分类", "分类异常",
								"指导", "其它指导", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanQianThirdInfo, fileName,
								f, chanQianThirdInfo);
					}
					if (style.equals("产前第四次随访")) {
						g += 1;
						String chanQianForthInfo[] = { "姓名", "身份证号", "孕周",
								"主诉", "体重", "宫底高度", "腹围", "胎位", "胎心率", "高压",
								"低压", "血红蛋白", "尿蛋白", "其他辅助检查", "分类", "分类异常",
								"指导", "其它指导", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanQianForthInfo, fileName,
								g, chanQianForthInfo);
					}
					if (style.equals("产前第五次随访")) {
						h += 1;
						String chanQianFifthInfo[] = { "姓名", "身份证号", "孕周",
								"主诉", "体重", "宫底高度", "腹围", "胎位", "胎心率", "高压",
								"低压", "血红蛋白", "尿蛋白", "其他辅助检查", "分类", "分类异常",
								"指导", "其它指导", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanQianFifthInfo, fileName,
								h, chanQianFifthInfo);
					}
					if (style.equals("产后随访")) {
						q += 1;
						String chanHouInfo[] = { "姓名", "身份证号", "体温", "一般健康情况",
								"一般心理状况", "高压", "低压", "乳房", "乳房异常", "恶露",
								"恶露异常", "子宫", "子宫异常", "伤口", "伤口异常", "其他", "分类",
								"分类异常", "指导", "其它指导", "转诊", "转诊原因", "机构及科室",
								"下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanHouInfo, fileName, q,
								chanHouInfo);
					}
					if (style.equals("产后四十二天随访")) {
						r += 1;
						String chanHouSiShiErInfo[] = { "姓名", "身份证号", "一般健康情况",
								"一般心理状况", "高压", "低压", "乳房", "乳房异常", "恶露",
								"恶露异常", "子宫", "子宫异常", "伤口", "伤口异常", "其他", "分类",
								"分类未恢复", "指导", "其它指导", "处理", "转诊原因", "机构及科室",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfChanHouSiShiErInfo, fileName,
								r, chanHouSiShiErInfo);
					}
					if (style.equals("新生儿随访")) {
						s += 1;
						String newChildInfo[] = { "姓名", "身份证号", "性别", "出生日期",
								"家庭住址", "出生孕周", "父亲姓名", "父亲职业", "父亲联系电话",
								"父亲出生日期", "母亲姓名", "母亲职业", "母亲联系电话", "母亲出生日期",
								"母亲妊娠期患病情况", "母亲妊娠期患病情况其它", "助产机构名称", "出生情况",
								"出生情况其他", "新生儿窒息", "畸型", "新生儿听力筛查", "新生儿疾病筛查",
								"新生儿出生体重", "目前体重", "出生身长", "喂养方式", "吃奶量",
								"吃奶次数", "呕吐", "大便", "大便次数", "体温", "脉率", "呼吸频率",
								"面色", "面色其他", "黄疸部位", "前囟一", "前囟二", "前囟",
								"前囟其他", "眼外观", "四肢活动度", "耳外观", "颈部包块", "鼻",
								"皮肤", "皮肤其他", "口腔", "肛门", "心肺听诊", "外生殖器",
								"腹部触诊", "脊柱", "脐带", "脐带其他", "指导", "转诊", "转诊原因",
								"机构及科室", "下次随访日期", "随访医生签名", "下次随访地点", "指导意见" };
						exportArchivesInfo(sheetOfNewChildInfo, fileName, s,
								newChildInfo);
					}
					if (style.equals("新生儿满月随访")) {
						t += 1;
						String newChildManYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "头围", "面色", "皮肤", "前囟一",
								"前囟二", "前囟闭合与否", "颈部包块", "眼外观", "耳外观", "出牙数",
								"心肺", "腹部", "脐部", "脐部其他", "四肢", "可疑佝偻病体征",
								"肛门/外生殖器", "血红蛋白值", "户外活动", "服用维生素D", "发育评估",
								"两次随访间患病情况", "其他", "指导", "转诊", "转诊原因", "机构及科室",
								"下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildManYueInfo, fileName,
								t, newChildManYueInfo);
					}
					if (style.equals("新生儿三月龄随访")) {
						w += 1;
						String newChildSanYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "头围", "面色", "皮肤", "前囟一",
								"前囟二", "前囟闭合与否", "颈部包块", "眼外观", "耳外观", "出牙数",
								"心肺", "腹部", "脐部", "可疑佝偻病症状", "四肢", "可疑佝偻病体征",
								"肛门/外生殖器", "血红蛋白值", "户外活动", "服用维生素D", "发育评估",
								"两次随访间患病情况", "其他", "指导", "转诊", "转诊原因", "机构及科室",
								"下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildSanYueInfo, fileName,
								w, newChildSanYueInfo);
					}
					if (style.equals("新生儿六月龄随访")) {
						x += 1;
						String newChildLiuYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "头围", "面色", "皮肤", "前囟一",
								"前囟二", "前囟闭合与否", "颈部包块", "眼外观", "耳外观", "出牙数",
								"心肺", "腹部", "听力", "可疑佝偻病症状", "四肢", "可疑佝偻病体征",
								"肛门/外生殖器", "血红蛋白值", "户外活动", "服用维生素D", "发育评估",
								"两次随访间患病情况", "其他", "指导", "转诊", "转诊原因", "机构及科室",
								"下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildLiuYueInfo, fileName,
								x, newChildLiuYueInfo);
					}
					if (style.equals("新生儿八月龄随访")) {
						y += 1;
						String newChildBaYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "头围", "面色", "皮肤", "前囟一",
								"前囟二", "前囟闭合与否", "眼外观", "耳外观", "出牙数", "心肺",
								"腹部", "可疑佝偻病症状", "四肢", "可疑佝偻病体征", "肛门/外生殖器",
								"血红蛋白值", "户外活动", "服用维生素D", "发育评估", "两次随访间患病情况",
								"其他", "指导", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildBaYueInfo, fileName,
								y, newChildBaYueInfo);
					}
					if (style.equals("新生儿十二月龄随访")) {
						u += 1;
						String newChildShiErYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "面色", "皮肤", "出牙数", "龋齿数",
								"前囟一", "前囟二", "前囟闭合与否", "听力", "眼外观", "耳外观",
								"心肺", "腹部", "四肢", "可疑佝偻病体征", "户外活动", "服用维生素D",
								"发育评估", "两次随访间患病情况", "其他", "指导", "转诊", "转诊原因",
								"机构及科室", "下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildShiErYueInfo,
								fileName, u, newChildShiErYueInfo);
					}
					if (style.equals("新生儿十八月龄随访")) {
						z += 1;
						String newChildShiBaYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "面色", "皮肤", "出牙数", "龋齿数",
								"前囟一", "前囟二", "前囟闭合与否", "步态", "眼外观", "耳外观",
								"心肺", "腹部", "四肢", "可疑佝偻病体征", "血红蛋白值", "户外活动",
								"服用维生素D", "发育评估", "两次随访间患病情况", "其他", "指导",
								"转诊", "转诊原因", "机构及科室", "下次随访日期", "随访医生签名",
								"指导意见" };
						exportArchivesInfo(sheetOfNewChildShiBaYueInfo,
								fileName, z, newChildShiBaYueInfo);
					}
					if (style.equals("新生儿二十四月龄随访")) {
						a += 1;
						String newChildErShiSiYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "面色", "皮肤", "出牙数", "龋齿数",
								"前囟一", "前囟二", "前囟闭合与否", "听力", "步态", "眼外观",
								"耳外观", "心肺", "腹部", "四肢", "可疑佝偻病体征", "户外活动",
								"服用维生素D", "发育评估", "两次随访间患病情况", "其他", "指导",
								"转诊", "转诊原因", "机构及科室", "下次随访日期", "随访医生签名",
								"指导意见" };
						exportArchivesInfo(sheetOfNewChildErShiSiYueInfo,
								fileName, a, newChildErShiSiYueInfo);
					}
					if (style.equals("新生儿三十月龄随访")) {
						b += 1;
						String newChildSanShiYueInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "面色", "皮肤", "出牙数", "龋齿数",
								"听力", "步态", "眼外观", "耳外观", "心肺", "腹部", "四肢",
								"户外活动", "两次随访间患病情况", "其他", "指导", "转诊", "转诊原因",
								"机构及科室", "下次随访日期", "随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildSanShiYueInfo,
								fileName, b, newChildSanShiYueInfo);
					}
					if (style.equals("新生儿三岁龄随访")) {
						v += 1;
						String newChildSanSuiInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "体格发育评价", "出牙数", "龋齿数",
								"听力", "心肺", "腹部", "血红蛋白值", "其他", "两次随访间患病情况",
								"两次随访间其他患病情况", "肺炎次数", "腹泻次数", "外伤次数", "指导",
								"指导其他", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildSanSuiInfo, fileName,
								v, newChildSanSuiInfo);
					}
					if (style.equals("新生儿四岁龄随访")) {
						c += 1;
						String newChildSiSuiInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "体格发育评价", "出牙数", "龋齿数",
								"视力", "心肺", "腹部", "血红蛋白值", "其他", "两次随访间患病情况",
								"两次随访间其他患病情况", "肺炎次数", "腹泻次数", "外伤次数", "指导",
								"指导其他", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildSiSuiInfo, fileName,
								c, newChildSiSuiInfo);
					}
					if (style.equals("新生儿五岁龄随访")) {
						d += 1;
						String newChildWuSuiInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "体格发育评价", "出牙数", "龋齿数",
								"视力", "心肺", "腹部", "血红蛋白值", "其他", "两次随访间患病情况",
								"两次随访间其他患病情况", "肺炎次数", "腹泻次数", "外伤次数", "指导",
								"指导其他", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildWuSuiInfo, fileName,
								d, newChildWuSuiInfo);
					}
					if (style.equals("新生儿六岁龄随访")) {
						e += 1;
						String newChildLiuSuiInfo[] = { "姓名", "身份证号", "体重",
								"体重等级", "身长", "身长等级", "体格发育评价", "出牙数", "龋齿数",
								"视力", "心肺", "腹部", "血红蛋白值", "其他", "两次随访间患病情况",
								"两次随访间其他患病情况", "肺炎次数", "腹泻次数", "外伤次数", "指导",
								"指导其他", "转诊", "转诊原因", "机构及科室", "下次随访日期",
								"随访医生签名", "指导意见" };
						exportArchivesInfo(sheetOfNewChildLiuSuiInfo, fileName,
								e, newChildLiuSuiInfo);
					}
				}
			}
			wbook.write();
			if (flag) {
				if (uploadExcel(path)) {
					result = true;
				} else {
					return;
				}

			} else {
				toastInformation(R.string.toast_no_record);
				return;
			}
			wbook.close();
			os.close();
			// cur.close();
		} catch (RowsExceededException e) {
			Log.e(this.getClass().getSimpleName(), "RowsExceededException");
			e.printStackTrace();
			result = false;
		} catch (FileNotFoundException e) {
			Log.e(this.getClass().getSimpleName(), "FileNotFoundException");
			e.printStackTrace();
			result = false;
		} catch (WriteException e) {
			Log.e(this.getClass().getSimpleName(), "WriteException");
			e.printStackTrace();
			result = false;
		} catch (IOException e) {
			Log.e(this.getClass().getSimpleName(), "IOException");
			e.printStackTrace();
			result = false;
		}
		if (result) {
			this.toastInformation(R.string.toast_export_success);
		} else {
			this.toastInformation(R.string.toast_export_failed);
		}
	}

	/**
	 * 往导出的excel中写数据
	 * 
	 * @param sheet
	 *            excel的sheet
	 * @param fileName
	 *            从哪一个文件获取数据
	 * @param row
	 *            第几行
	 * @param str
	 *            标题数组
	 * @throws RowsExceededException
	 * @throws WriteException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void exportArchivesInfo(WritableSheet sheet, String fileName,
			int row, String[] str) throws RowsExceededException,
			WriteException, FileNotFoundException, IOException {
		Properties properties = new Properties();
		File file = new File(Environment.getExternalStorageDirectory()
				+ "/health/" + fileName + ".txt");
		Log.i(this.getClass().getSimpleName(), "路径" + file.getAbsolutePath());
		properties.load(new FileReader(file.getAbsolutePath()));
		WritableCellFormat title = new WritableCellFormat();

		for (int i = 0; i < str.length; i++) {
			Log.i(this.getClass().getSimpleName(), "添加内容");
			/* 获取key */
			String key = str[i];
			/* 根据键得到对应的值value */
			String value = properties.getProperty(key);
			// 标题栏
			if (row == 1) {
				Label t = new Label(i, 0, key, title);
				sheet.addCell(t);
			}
			sheet.addCell(new Label(i, row, value));
			Log.i(this.getClass().getSimpleName(), key + ":" + value);
		}

	}

	/**
	 * 上传excel
	 * 
	 * @param path
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public boolean uploadExcel(String path) throws FileNotFoundException,
			IOException {
		Properties properties = new Properties();
		int ftpPort;
		File file = new File(Environment.getExternalStorageDirectory()
				+ "/health/ftp.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		Log.i(this.getClass().getSimpleName(), "路径" + file.getAbsolutePath());
		properties.load(new FileReader(file.getAbsolutePath()));
		String ftpAddress = properties.getProperty("FTP地址");
		String StrFtpPort = properties.getProperty("端口号");
		String ftpPath = properties.getProperty("保存路径");
		String ftpUser = properties.getProperty("用户名");
		String ftpPass = properties.getProperty("密码");
		if (Util.allIsNotEmpty(ftpAddress, StrFtpPort, ftpPath, ftpUser,
				ftpPass)) {
			ftpPort = Integer.parseInt(StrFtpPort);
		} else {
			this.toastInformation(R.string.export_setting_not_set);
			return false;
		}
		Util.upload(ftpAddress, ftpPort, ftpUser, ftpPass, path, ftpPath);
		return true;
	}

}
